package com.etb.wisdom.eframe.dict.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.etb.core.pojo.CriteriaType;
import com.etb.core.pojo.Page;
import com.etb.core.service.ETBService;
import com.etb.core.utils.SequenceUtil;
import com.etb.wisdom.eframe.dict.service.EtbDicttypeService;
import com.etb.wisdom.eframe.dict.vo.DictTreeGridNode;
import com.etb.wisdom.eframe.model.EtbDicttype;

@Service
public class EtbDicttypeServiceImpl extends ETBService implements EtbDicttypeService {

	public List<DictTreeGridNode> getTreeGridNode(CriteriaType criteriaType, Page page) {
		List<DictTreeGridNode> treedata = new ArrayList<DictTreeGridNode>();
		List<EtbDicttype> types = service.query(criteriaType, page);
		for(EtbDicttype type: types) {
			DictTreeGridNode node = new DictTreeGridNode(type.getId(), type.getTypename(), type.getTypecode());
			node.set_parentId(type.getUplevelid());
			node.setState(DictTreeGridNode.State.closed.toString());
			treedata.add(node);
		}
		return treedata;
	}
	
	@Override
	public void save(EtbDicttype type) {
		service.save(type);
		if(type.getUplevelid() == null) {
			type.setTypeseq(type.getId());
		} else {
			EtbDicttype uplevelType = new EtbDicttype();
			uplevelType.setId(type.getUplevelid());
			service.extend(uplevelType);
			type.setTypeseq(SequenceUtil.generateNewSequence(uplevelType.getTypeseq(), type.getId()));
		}
		service.save(type);
	}
}
